/*
 * Copyright 2011 Daniel Krieg
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.code.csf;

import com.google.code.csf.aspects.LoggingFormatter;
import com.google.code.csf.aspects.ServiceProxyLoggingAspect;
import com.google.code.csf.locators.ServiceHolder;
import com.google.code.csf.locators.ServiceLocatorChain;
import com.google.code.csf.locators.ejb.LocalSlsbServiceLocator;
import com.google.code.csf.locators.ejb.RemoteSlsbServiceLocator;
import com.google.code.csf.locators.jaxrpc.JBossJaxRpcServiceFactoryServiceLocator;
import com.google.code.csf.locators.jaxrpc.JaxRpcServiceFactoryServiceLocator;
import com.google.code.csf.locators.jaxrpc.JaxRpcServiceRefServiceLocator;
import com.google.code.csf.locators.jaxws.JaxWsServiceRefServiceLocator;
import com.google.code.csf.locators.jaxws.JaxWsServiceServiceLocator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class ServiceTemplateBuilder {
    private List serviceLocators = new ArrayList();
    private final Class serviceTypeClass;
    private ServiceProxyLoggingAspect loggingAspect;

    public static  ServiceTemplateBuilder templateFor(Class serviceTypeClass) {
        return new ServiceTemplateBuilder(serviceTypeClass);
    }

    private ServiceTemplateBuilder(Class serviceTypeClass) {
        this.serviceTypeClass = serviceTypeClass;
    }

    public ServiceTemplateBuilder withJBossJaxRpcServiceFactoryServiceLocator(URL wsdlDocumentUrl, URL jaxrpcMappingDocumentUrl, Class portInterface, String endpointAddress) {
        JBossJaxRpcServiceFactoryServiceLocator locator = new JBossJaxRpcServiceFactoryServiceLocator();
        locator.setJaxrpcMappingDocumentUrl(jaxrpcMappingDocumentUrl);
        return withJaxRpcServiceFactoryServiceLocator(locator, wsdlDocumentUrl, portInterface, endpointAddress);
    }

    public ServiceTemplateBuilder withJaxRpcServiceFactoryServiceLocator(URL wsdlDocumentUrl, Class portInterface, String endpointAddress) {
        return withJaxRpcServiceFactoryServiceLocator((Class) null, wsdlDocumentUrl, portInterface, endpointAddress);
    }

    public ServiceTemplateBuilder withJaxRpcServiceFactoryServiceLocator(Class serviceFactoryClass, URL wsdlDocumentUrl, Class portInterface, String endpointAddress) {
        JaxRpcServiceFactoryServiceLocator locator = new JaxRpcServiceFactoryServiceLocator();
        locator.setServiceFactoryClass(serviceFactoryClass);
        return withJaxRpcServiceFactoryServiceLocator(locator, wsdlDocumentUrl, portInterface, endpointAddress);
    }

    public ServiceTemplateBuilder withJaxRpcServiceRefServiceLocator(Class portInterface, String endpointAddress) {
        JaxRpcServiceRefServiceLocator locator = new JaxRpcServiceRefServiceLocator();
        locator.setPortInterface(portInterface);
        locator.setEndpointAddress(endpointAddress);
        return withServiceLocator(locator);
    }

    public ServiceTemplateBuilder withJaxWsServiceServiceLocator(URL wsdlDocumentUrl, Class portInterface, String endpointAddress) {
        JaxWsServiceServiceLocator locator = new JaxWsServiceServiceLocator();
        locator.setWsdlDocumentUrl(wsdlDocumentUrl);
        locator.setPortInterface(portInterface);
        locator.setEndpointAddress(endpointAddress);
        return withServiceLocator(locator);
    }

    public ServiceTemplateBuilder withJaxWsServiceRefServiceLocator(Class portInterface, String endpointAddress) {
        JaxWsServiceRefServiceLocator locator = new JaxWsServiceRefServiceLocator();
        locator.setPortInterface(portInterface);
        locator.setEndpointAddress(endpointAddress);
        return withServiceLocator(locator);
    }

    public ServiceTemplateBuilder withRemoteSlsbServiceLocator(Class expectedType, String jndiName, Properties environment) {
        return withRemoteSlsbServiceLocator(new RemoteSlsbServiceLocator(environment), expectedType, jndiName, false);
    }

    public ServiceTemplateBuilder withRemoteSlsbServiceLocator(Class expectedType, String jndiName, boolean resourceRef) {
        return withRemoteSlsbServiceLocator(new RemoteSlsbServiceLocator(), expectedType, jndiName, resourceRef);
    }

    public ServiceTemplateBuilder withLocalSlsbServiceLocator(Class expectedType, String jndiName) {
        LocalSlsbServiceLocator locator = new LocalSlsbServiceLocator();
        locator.setExpectedType(expectedType);
        locator.setJndiName(jndiName);
        locator.setResourceRef(true);
        return withServiceLocator(locator);
    }

    public ServiceTemplateBuilder withServiceHolder(Object service) {
        return withServiceLocator(new ServiceHolder(service));
    }

    public ServiceTemplateBuilder withServiceLocator(ServiceLocator locator) {
        serviceLocators.add(locator);
        return this;
    }

    public ServiceTemplateBuilder withLoggingAspect(Logger logger, Level logLevel, Level errorLevel, LoggingFormatter formatter) {
        loggingAspect = new ServiceProxyLoggingAspect(serviceTypeClass, logger, logLevel, errorLevel, formatter);
        return this;
    }

    public ServiceTemplateBuilder withLoggingAspect(Logger logger, LoggingFormatter formatter) {
        loggingAspect = new ServiceProxyLoggingAspect(serviceTypeClass, logger, formatter);
        return this;
    }

    public ServiceTemplateBuilder withLoggingAspect(LoggingFormatter formatter) {
        loggingAspect = new ServiceProxyLoggingAspect(serviceTypeClass, formatter);
        return this;
    }

    public ServiceTemplate build() {
        ServiceTemplate template = new ServiceTemplate();
        template.setTypeToLocate(serviceTypeClass);
        if(loggingAspect == null) {
            template.setServiceLocator(new ServiceLocatorChain(serviceLocators));
        } else {
            template.setServiceLocator((ServiceLocator) loggingAspect.newServiceInstance(new ServiceLocatorChain(serviceLocators)));
        }
        return template;
    }

    private ServiceTemplateBuilder withRemoteSlsbServiceLocator(RemoteSlsbServiceLocator locator, Class expectedType, String jndiName, boolean resourceRef) {
        locator.setExpectedType(expectedType);
        locator.setJndiName(jndiName);
        locator.setResourceRef(resourceRef);
        locator.setHomeInterface(expectedType);
        return withServiceLocator(locator);
    }

    private ServiceTemplateBuilder withJaxRpcServiceFactoryServiceLocator(JaxRpcServiceFactoryServiceLocator locator, URL wsdlDocumentUrl, Class portInterface, String endpointAddress) {
        locator.setWsdlDocumentUrl(wsdlDocumentUrl);
        locator.setPortInterface(portInterface);
        locator.setEndpointAddress(endpointAddress);
        return withServiceLocator(locator);
    }
}
